package com.cookie.meetproject.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.cookie.framework.base.BaseUIActivity;
import com.cookie.framework.helper.FileHelper;
import com.cookie.meetproject.R;
import com.uuzuche.lib_zxing.activity.CaptureFragment;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import androidx.annotation.Nullable;

/**
 * 项目名:    MeetProject
 * 包名:      com.cookie.meetproject.ui
 * 文件名:    QrCodeActivity
 * 创建者:    cookie
 * 创建时间:   2020/5/21 23:20
 * 描述:       二维码扫描界面
 */

public class QrCodeActivity extends BaseUIActivity implements View.OnClickListener {

    private static final int REQUEST_IMAGE = 1234;

    private CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() {
        @Override
        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_SUCCESS);
            bundle.putString(CodeUtils.RESULT_STRING, result);
            resultIntent.putExtras(bundle);
            setResult(RESULT_OK, resultIntent);
            finish();
        }

        @Override
        public void onAnalyzeFailed() {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_FAILED);
            bundle.putString(CodeUtils.RESULT_STRING, "");
            resultIntent.putExtras(bundle);
            setResult(RESULT_OK, resultIntent);
            finish();
        }
    };

    private FrameLayout flMyContainer;
    private ImageView ivBack;
    private TextView ivToAblum;
    private ImageView ivFlashlight;

    private boolean isOpenLight = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qrcode);
        initQrCode();
        initView();
    }

    private void initQrCode() {

        CaptureFragment captureFragment = new CaptureFragment();
        CodeUtils.setFragmentArgs(captureFragment, R.layout.layout_qrcode);
        captureFragment.setAnalyzeCallback(analyzeCallback);
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_my_container, captureFragment).commit();
    }

    private void initView() {
        flMyContainer = (FrameLayout) findViewById(R.id.fl_my_container);
        ivBack = (ImageView) findViewById(R.id.iv_back);
        ivToAblum = (TextView) findViewById(R.id.iv_to_ablum);
        ivFlashlight = (ImageView) findViewById(R.id.iv_flashlight);

        ivBack.setOnClickListener(this);
        ivFlashlight.setOnClickListener(this);
        ivToAblum.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_flashlight:
                isOpenLight = !isOpenLight;
                CodeUtils.isLightEnable(isOpenLight);
                ivFlashlight.setImageResource(isOpenLight ? R.drawable.img_flashlight_p
                        : R.drawable.img_flashlight);
                break;
            case R.id.iv_to_ablum:
                openAlbum();
                break;

        }
    }

    //打开相册
    private void openAlbum() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("image/*");
        startActivityForResult(intent, REQUEST_IMAGE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode == REQUEST_IMAGE) {
            if (data != null) {
                Uri uri = data.getData();
                String path = FileHelper.getInstance().getRealPathFromURI(QrCodeActivity.this, uri);
                try {
                    CodeUtils.analyzeBitmap(path, new CodeUtils.AnalyzeCallback() {
                        @Override
                        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
                            analyzeCallback.onAnalyzeSuccess(mBitmap, result);
                        }

                        @Override
                        public void onAnalyzeFailed() {
                            analyzeCallback.onAnalyzeFailed();
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
